<!-- @Author: Yu_Bo -->
<template>
	<view class="setup">
		<view class="setup_txt" v-for="(item,index) in list" :key="index" @click="pathBtn(item)">
			<view class="left">
				{{item.name}}
			</view>
			<view class="right" v-if="item.type==2">
				<u-input v-model="item.value" type="text" @blur="blurBtn" :clearable="false" :placeholder-style="placeholderStyle" placeholder="请输入真实姓名" />
			</view>
			<view class="right" v-else>
				<view class="line_one" v-if="item.type==1">
					{{formatMobile(item.txt)}}
				</view>
				<view class="right_txt line_one" v-if="item.type==3">
					{{item.txt}}
				</view>
				<image src="@/static/images/img/right_icon.png" mode=""></image>
				<button v-if="item.type==1" open-type="getPhoneNumber" @getphonenumber="getphonenumber"></button>
			</view>
		</view>
		<!-- 底部按钮 -->
		<c-footer>
			<view class="footer_btn">
				<view class="btn_txt color_right page_button" @click="quitBtn">
					退出登录
				</view>
			</view>
		</c-footer>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		components: {},
		data() {
			return{
				placeholderStyle: "color: #999999",
				loading: false
			}
		},
		computed: {
			...mapState({
				userinfo: (state) => state.userinfo,
			}),
			schoolinfo() {
				if(this.userinfo.college && this.userinfo.grade && this.userinfo.speciality) {
					return this.userinfo.college + '/' + this.userinfo.grade + '/' + this.userinfo.speciality
				} else {
					return ""
				}
			},
			list() {
				return [
					{
						name: "绑定手机号",
						txt: this.userinfo?.mobile || "",
						path: "",
						type: 1,
					},
					{
						name: "真实姓名",
						txt: "",
						path: "",
						value: this.userinfo.nickname || "",
						type: 2,
					},
					{
						name: "学籍信息",
						txt: this.schoolinfo || "请选择",
						path: "/pagesWo/student/index",
						type: 3,
					},
					{
						name: "意见反馈",
						txt: "",
						path: "/pagesWo/opinion/index",
						type: 4,
					},
					{
						name: "用户协议",
						txt: "",
						path: "/pagesWo/protocol/index",
						type: 4,
					}
				]
			}
		},
		onLoad() {},
		onShow() {},
		methods: {
			formatMobile(value){
				return value ? value.substring(0, 3) + "****" + value.substring(7, 11) : '去绑定'
			},
			// 跳转
			pathBtn(item) {
				if(item.type ==1){
					return
				}
				if(item.type == 3) {
					this.$navTo(item.path)
				}
			},
			// 绑定手机号
			getphonenumber(e){
				var that = this
				if(e.detail.errMsg=='getPhoneNumber:ok'){
					if(that.loading) return
					that.loading = true
					var params = {
						code: e.detail.code
					}
					that.$httpApi.getPhone(params).then((res) => {
						console.log(res)
						that.loading = false
						if (res.code == 1) {
							if(res.data.errmsg == "ok") {
								var obj = {
									mobile: res.data.phone_info.phoneNumber
								}
								that.getEdit(obj)
							}
						}
					})
				}
			},
			// 修改姓名
			blurBtn() {
				if(this.list[1].value == "") {
					that.$tips.showToast('请输入真是姓名')
					return
				}
				var params = {
					username: this.list[1].value
				}
				this.getEdit(params)
			},
			getEdit(params) {
				var that = this
				that.$httpApi.myBinding(params).then((res) => {
					that.loading = false
					if (res.code == 1) {
						that.$tips.showToast(res.msg)
						// 获取个人信息
						this.$store.dispatch('getUserInfo')
					}
				})
			},
			// 退出登录
			quitBtn() {
				this.$store.commit("DEL_TOKEN", "")
				this.$store.commit("DEL_ADMIN", "")
				this.$store.commit("DEL_INFO", "")
				uni.navigateBack()
			},
		},
	}
</script>

<style lang='scss' scoped>
	.setup {
		width: 100%;
		padding: 30rpx 30rpx;
		
		.setup_txt {
			width: 100%;
			height: 112rpx;
			margin-bottom: 30rpx;
			padding: 0 30rpx;
			background: #F6F6F6;
			border-radius: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.left {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #666666;
			}
			
			.right {
				position: relative;
				width: 450rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #999999;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				
				.right_txt {
					width: 400rpx;
					text-align: right;
				}
				
				image {
					display: block;
					width: 28rpx;
					height: 28rpx;
					margin-left: 10rpx;
				}
				
				button {
					position: absolute;
					top: 0;
					left: 0;
					display: block;
					width: 100%;
					height: 100%;
					opacity: 0;
				}
				
				u-input {
					width: 100%;;
				}
				::v-deep .u-input{
					width: 100%;
					
					.u-input__input {
						width: 100%;
						text-align: right;
					}
				}
			}
		}
		
		.footer_btn {
			width: 100%;
			padding: 30rpx 30rpx;
			background: #fff;
			
			.btn_txt {
				width: 100%;
				height: 90rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
				color: #FFFFFF;
				border-radius: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>